﻿package no.doomsday.aronning.engines.verlets  {
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import no.doomsday.aronning.engines.verlets.forces.ConstantForce;
	import no.doomsday.aronning.engines.verlets.utils.Cell;
	import no.doomsday.aronning.engines.verlets.utils.LinkedListNode;
	
	/**
	* ...
	* @author Andreas Rønning
	*/
	public class Verlet extends LinkedListNode implements IRenderable, IConstrainable{
		private static var UID_POOL:uint = 0;
		private var _x:Number = 0;
		private var _y:Number = 0;
		private var _z:Number = 0;
		public var oldX:Number = 0;
		public var oldY:Number = 0;
		public var oldZ:Number = 0;
		public var data:*;
		public var uid:uint = UID_POOL++;
		public var dead:Boolean = false;
		public var prevVX:Number = 0;
		public var prevVY:Number = 0;
		public function Verlet(initialX:Number = 0, initialY:Number = 0, initialZ:Number = 0) {
			_x = initialX;
			_y = initialY;
			_z = initialZ;
			oldX = initialX;
			oldY = initialY;
			oldZ = initialZ;
		}
		public function set x(value:Number):void {
			//oldX = _x;
			_x = value;
		}
		public function get x():Number {
			return _x;
		}
		public function set y(value:Number):void {
			//oldY = _y;
			_y = value;
		}
		public function get y():Number {
			return _y;
		}
		
		public function set z(value:Number):void {
			//oldY = _z;
			_z = value;
		}
		public function get z():Number {
			return _z;
		}
		public function render(graphics:Graphics):void {
			graphics.beginFill(0);
			graphics.drawCircle(_x, _y, 2);
			graphics.endFill();
		}
		public function update(timeStep:Number, accumulator:ConstantForce):void {
			var tempY:Number = _y;
			var tempX:Number = _x;
			
			prevVX = _x - oldX + accumulator.x * timeStep * timeStep;
			prevVY = _y - oldY + accumulator.y * timeStep * timeStep;
			
			_x += prevVX;
			_y += prevVY;
			
			oldY = tempY;
			oldX = tempX;
			
		}
		override public function destroy():void 
		{
			dead = true;
			super.destroy();
		}
		public function moveTo(x:Number, y:Number):void {
			this.x = oldX = x;
			this.y = oldY = y;
		}
		public function toPoint():Point {
			return new Point(x, y);
		}
		public function toString():String {
			var str:String = "Verlet: ";
			str += "x: " + x;
			str += "y: " + y;
			return str;
		}
		
	}
	
}